/*************************************************************************/
/** Copyright.                                                          **/
/** FileName: SetMenu.c                                                 **/
/** Author: kongls                                                      **/
/** Version: 1.0.0.0                                                    **/
/** Description: SetMenu                                                **/
/*************************************************************************/
//=======================================================================//
//= Include files.                                                      =//
//=======================================================================//
#include "DemoProc.h"
#include "SGUI_Menu.h"
#include "SGUI_FontResource.h"
#include "HMI_Engine.h"
#include "Resource.h"

//=======================================================================//
//= Static function declaration.                                        =//
//=======================================================================//
static HMI_ENGINE_RESULT    HMI_SetMenu_Initialize(SGUI_SCR_DEV* pstDeviceIF);
static HMI_ENGINE_RESULT    HMI_SetMenu_Prepare(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters);
static HMI_ENGINE_RESULT    HMI_SetMenu_RefreshScreen(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters);
static HMI_ENGINE_RESULT    HMI_SetMenu_ProcessEvent(SGUI_SCR_DEV* pstDeviceIF, const HMI_EVENT_BASE* pstEvent, SGUI_INT* piActionID);
static HMI_ENGINE_RESULT    HMI_SetMenu_PostProcess(SGUI_SCR_DEV* pstDeviceIF, HMI_ENGINE_RESULT eProcResult, SGUI_INT iActionID);

//=======================================================================//
//= Static variable declaration.                                        =//
//=======================================================================//
static SGUI_ITEMS_ITEM      s_arrstMenuItems[] =        {   {SCR6_MENU_ITEM1, NULL},
                                                            {SCR6_MENU_ITEM2, NULL},
                                                            {SCR6_MENU_ITEM3, NULL},
                                                            {SCR6_MENU_ITEM4, NULL},
                                                            {SCR6_MENU_ITEM5, NULL}
                                                        };

static SGUI_ITEMS_ITEM      s_arrstSubMenuItems[] =     {   {SCR6_SUB_MENU_ITEM1, NULL},
                                                            {SCR6_SUB_MENU_ITEM2, NULL},
                                                            {SCR6_SUB_MENU_ITEM3, NULL},
                                                            {SCR6_SUB_MENU_ITEM4, NULL}
                                                        };

static SGUI_MENU        s_stDemoMenuObject =        {0x00};
static SGUI_MENU        s_stDemoSubMenuObject =     {0x00};

static SGUI_MENU*   s_pstActivedMenu =          &s_stDemoMenuObject;

extern char time_str[64];
extern char battery_str[3];

//=======================================================================//
//= Global variable declaration.                                        =//
//=======================================================================//
HMI_SCREEN_ACTION       s_stSetMenuActions =            {   HMI_SetMenu_Initialize,
                                                            HMI_SetMenu_Prepare,
                                                            HMI_SetMenu_RefreshScreen,
                                                            HMI_SetMenu_ProcessEvent,
                                                            HMI_SetMenu_PostProcess
                                                        };
HMI_SCREEN_OBJECT       g_stHMISet_Menu =               {   HMI_SCREEN_ID_SetMenu_PAINT,
                                                            &s_stSetMenuActions
                                                        };

//=======================================================================//
//= Function define.                                                    =//
//=======================================================================//
HMI_ENGINE_RESULT HMI_SetMenu_Initialize(SGUI_SCR_DEV* pstDeviceIF)
{
    // /*----------------------------------*/
    // /* Variable Declaration             */
    // /*----------------------------------*/
    SGUI_RECT               stLayout;

    /*----------------------------------*/
    /* Initialize                       */
    /*----------------------------------*/
    stLayout.iX =           1;
    stLayout.iY =           1;
    stLayout.iWidth =       125;
    stLayout.iHeight =      159;

    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, time_str, &SGUI_DEFAULT_FONT_16, 1, 1, 120, SGUI_DRAW_NORMAL);

    /*----------------------------------*/
    /* Process                          */
    /*----------------------------------*/
    // Initialize list data.
    SGUI_SystemIF_MemorySet(&s_stDemoMenuObject, 0x00, sizeof(SGUI_MENU));
     //Initialize list object.
    SGUI_Menu_Initialize(&s_stDemoMenuObject, &stLayout, &GB2312_FZXS12,         s_arrstMenuItems, sizeof(s_arrstMenuItems)/sizeof(SGUI_ITEMS_ITEM));
    printf("11HMI_SetMenu_Initialize\n");
    return HMI_RET_NORMAL;
}

HMI_ENGINE_RESULT HMI_SetMenu_Prepare(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters)
{
    /*----------------------------------*/
    /* Process                          */
    /*----------------------------------*/

    if(NULL != pstDeviceIF->fnClear)
    {
        pstDeviceIF->fnClear();
    }

    SGUI_Basic_DrawRectangle1(pstDeviceIF, 0, 0, pstDeviceIF->stSize.iWidth, pstDeviceIF->stSize.iHeight, SGUI_COLOR_BKGCLR, SGUI_COLOR_BKGCLR);
    SGUI_Menu_Repaint(pstDeviceIF, &s_stDemoMenuObject);
    printf("HMI_SetMenu_Prepare\n");
    return HMI_RET_NORMAL;
}

HMI_ENGINE_RESULT HMI_SetMenu_RefreshScreen(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters)
{
    /*----------------------------------*/
    /* Process                          */
    /*----------------------------------*/
    SGUI_Menu_Repaint(pstDeviceIF, s_pstActivedMenu);
    return HMI_RET_NORMAL;
}

HMI_ENGINE_RESULT HMI_SetMenu_ProcessEvent(SGUI_SCR_DEV* pstDeviceIF, const HMI_EVENT_BASE* pstEvent, SGUI_INT* piActionID)
{
    /*----------------------------------*/
    /* Variable Declaration             */
    /*----------------------------------*/
    HMI_ENGINE_RESULT       eProcessResult;
    SGUI_UINT16             uiKeyCode;
    SGUI_UINT16             uiKeyValue;
    KEY_PRESS_EVENT*        pstKeyEvent;
    SGUI_INT                iProcessAction;
    SGUI_RECT               stItemArea;

    /*----------------------------------*/
    /* Initialize                       */
    /*----------------------------------*/
    eProcessResult =            HMI_RET_NORMAL;
    pstKeyEvent =               (KEY_PRESS_EVENT*)pstEvent;
    iProcessAction =            HMI_DEMO_PROC_NO_ACT;

    /*----------------------------------*/
    /* Process                          */
    /*----------------------------------*/
    // Check event is valid.
    if(SGUI_FALSE == HMI_EVENT_SIZE_CHK(*pstKeyEvent, KEY_PRESS_EVENT))
    {
        // Event data is invalid.
        eProcessResult = HMI_RET_INVALID_DATA;
    }
    else if(EVENT_ID_KEY_PRESS == pstEvent->iID)
    {
        uiKeyCode = pstKeyEvent->Data.uiKeyValue;
        uiKeyValue = KEY_CODE_VALUE(uiKeyCode);
        switch(uiKeyValue)
        {
            case KEY_VALUE_MENU:
            {
                iProcessAction = HMI_DEMO_PROC_CANCEL;
                printf("menu\n");
                break;
            }
            case KEY_VALUE_UP:
            {
                if(SGUI_Menu_GetSelection(s_pstActivedMenu)->iIndex > 0)
                {
                    SGUI_Menu_Selecte(s_pstActivedMenu, SGUI_Menu_GetSelection(s_pstActivedMenu)->iIndex-1);
                    SGUI_Menu_Repaint(pstDeviceIF, s_pstActivedMenu);
                }
                break;
            }
            case KEY_VALUE_DOWN:
            {
                if(SGUI_Menu_GetSelection(s_pstActivedMenu)->iIndex < s_pstActivedMenu->stItems.iCount-1)
                {
                    SGUI_Menu_Selecte(s_pstActivedMenu, SGUI_Menu_GetSelection(s_pstActivedMenu)->iIndex+1);
                    SGUI_Menu_Repaint(pstDeviceIF, s_pstActivedMenu);
                }
                break;
            }
            default:
            {
                break;
            }
        }
    }
    if(NULL != piActionID)
    {
        *piActionID = iProcessAction;
    }

    //printf("HMI_SetMenu_ProcessEvent time =%s\n",time_str);
    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, time_str, &SGUI_DEFAULT_FONT_16, 2, 2, 90, SGUI_DRAW_NORMAL);
    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, battery_str, &SGUI_DEFAULT_FONT_16, 91, 2, 120, SGUI_DRAW_NORMAL);
    SGUI_Basic_DrawLine(pstDeviceIF, 1, 21, 125, 21, SGUI_COLOR_FRGCLR);
    SGUI_Basic_DrawLine(pstDeviceIF, 1, 159, 125, 159, SGUI_COLOR_FRGCLR);

    return eProcessResult;
}

HMI_ENGINE_RESULT HMI_SetMenu_PostProcess(SGUI_SCR_DEV* pstDeviceIF, HMI_ENGINE_RESULT eProcResult, SGUI_INT iActionID)
{
    if(HMI_DEMO_PROC_CANCEL == iActionID)
    {
        HMI_GoBack(NULL);
    }

    return HMI_RET_NORMAL;
}
